import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import store from '@/store' //store 相当于组件里面 this.$store
import http from '@/utils/http'
const router = createRouter({
    // 默认首页 /   /category  /list /detail/:id   /user  /user/order  /user/profile /user/editpassword
    //在vue2里面这个配置项 叫mode 如果是开发环境 用hash模式 如果生产环境用history
    history: process.env.NODE_ENV === 'development' ? createWebHashHistory() : createWebHistory(),
    routes: [
        {
            path: "/login", component: () => import("@views/Login.vue")
        },
        {//后台模块
            path: "/manage", component: () => import("@views/Manage.vue"),meta:{title:"后台"},
            children: [
                {
                    path: "", component: () => import("@views/manage/Home.vue"),meta:{title:"首页"}
                },
                {
                    path:"question",component:()=>import("@views/manage/Question.vue"),meta:{title:"面试题"},
                    children:[
                        {path:"add",component:()=>import("@views/manage/question/Add.vue"),meta:{title:"添加面试题"}},
                        {path:"list",component:()=>import("@views/manage/question/List.vue"),meta:{title:"面试题列表"}}
                    ]
                },
                {
                    path:"answer",component:()=>import("@views/manage/Answer.vue"),
                    children:[
                        {path:"add",component:()=>import("@views/manage/answer/Add.vue")},
                        {path:"list",component:()=>import("@views/manage/answer/List.vue")}
                    ]
                },
                {
                    path:"category",component:()=>import("@views/manage/Category.vue"),
                    children:[
                        {path:"add",component:()=>import("@views/manage/category/Add.vue")},
                        {path:"list",component:()=>import("@views/manage/category/List.vue")}
                    ]
                },
                {
                    path:"user",component:()=>import("@views/manage/User.vue"),meta:{title:"用户管理"},
                    children:[
                        {path:"add",component:()=>import("@views/manage/user/Add.vue"),meta:{title:"添加用户"}},
                        {path:"list",component:()=>import("@views/manage/user/List.vue"),meta:{title:"用户列表"}},
                        {path:"edit/:id",component:()=>import("@views/manage/user/Edit.vue"),meta:{title:"修改用户信息"}},
                        {path:"auth",component:()=>import("@views/manage/user/Auth.vue"),meta:{title:"权限列表"}}
                    ]
                }
            ]
        },
        {
            // /ab_c :nomatch(.*)  /ab_c/abc/abc  :nomatch(.*)*
            path: "/:nomatch(.*)*", component: () => import("@views/NotFound.vue")
        }
    ]
});
router.beforeEach(async (to, from, next) => { //status  state
    if (store.getters['userStore/islogin']) {
        let re = await http.get("/user/veryfy", { Headers: { Authorization: store.state.userStore.Authorization } })
        // console.log("re==>",re);
        if (re.status == 400) {
            next("/login")
        } else {
            if (to.path.startsWith("/login")) {
                next("/manage");
            } else {
                next();
            }
        }
    } else {
        if (to.path.startsWith("/login")) {
            next();
        } else {
            next("/login");
        }
    }
});
export default router;